stop using QFile api to delete a single local file
authorMatthieu Gallien <matthieu.gallien@nextcloud.com>
Mon, 7 Apr 2025 13:37:41 +0000 (15:37 +0200)
committerbackportbot[bot] <backportbot[bot]@users.noreply.github.com>
Tue, 8 Apr 2025 21:05:06 +0000 (21:05 +0000)
Signed-off-by: Matthieu Gallien <matthieu.gallien@nextcloud.com>
src/common/filesystembase.cpp

index ffcd4f36bfe0ff7e1c9d9402d1cfd889c965e4ac..b63f6c1a7e156864d68897f792a55d7212913512 100644 (file)
@@ -26,6 +26,7 @@
 #include <QFile>
 #include <QCoreApplication>
 
+#include <filesystem>
 #include <sys/stat.h>
 #include <sys/types.h>
 
@@ -588,13 +589,30 @@ bool FileSystem::remove(const QString &fileName, QString *errorString)
     // allow that.
     setFileReadOnly(fileName, false);
 #endif
-    QFile f(fileName);
-    if (!f.remove()) {
+
+    try {
+        if (!std::filesystem::remove(std::filesystem::path{fileName.toUtf8().data()})) {
+            if (errorString) {
+                *errorString = QObject::tr("File is already deleted");
+            }
+            return false;
+        }
+    }
+    catch (const std::filesystem::filesystem_error &e)
+    {
         if (errorString) {
-            *errorString = f.errorString();
+            *errorString = QString::fromLatin1(e.what());
         }
         return false;
     }
+    catch (...)
+    {
+        if (errorString) {
+            *errorString = QObject::tr("Error deleting the file");
+        }
+        return false;
+    }
+
     return true;
 }